// 顶栏点击事件
const a = document.querySelector(".topPreset a")
a.addEventListener("touchstart", function () {
	history.back()
})

// 给input1设置change事件
const input1 = document.querySelector(".input1 input")
const input = document.querySelector(".input2 input")
const input2 = document.querySelector(".input2")
const btn = document.querySelector("button")
const btn1 = document.querySelector(".btn1")
const floatingTips = document.querySelector(".floatingTips")

let isAllsuccess = [false, false]
input1.addEventListener("change", function () {
	redBtn(true, 0)
})
input.addEventListener("change", function () {
	redBtn(false, 1)
})

// 换绑成功
btn.addEventListener("touchstart", function () {
	btn.innerText = btn1.innerText
	floatingTips.classList.add("TipsActiveClass")
})

function redBtn(bool, index) {
	isAllsuccess[index] = true
	!verify(bool) && (isAllsuccess[index] = false)
	isAllsuccess[0] && isAllsuccess[1]
		? (btn.style.backgroundColor = "#EF4F3F")
		: (btn.style.backgroundColor = "#FADCD9")
}

/* 
    @boo:true(返回手机号验证结果)
         false(返回验证码验证结果)
*/
function verify(boo) {
	return boo
		? verifyCode(
				/^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
				input1,
				document.querySelector(".error")
		  )
		: verifyCode(/^\d{6}$/, input, input2.nextElementSibling)
	//内部函数
	function verifyCode(regStr, ele, errInfoEle) {
		const reg = regStr
		const flag = reg.test(ele.value)
		errInfoEle.style.display = flag ? "none" : "block"
		return flag
	}
}

// 获取验证码
const span = document.querySelector(".input2 span")
let iscountDown = false
span.addEventListener("click", function () {
	if (iscountDown) return
	const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
	const flag = reg.test(input1.value)
	if (!flag) return
	let countDown = 3
	span.innerHTML = `${countDown}s后重新获取`
	iscountDown = true
	let setId = setInterval(function () {
		countDown--
		span.innerHTML = `${countDown}s后重新获取`

		if (countDown < 0) {
			clearInterval(setId)
			span.innerText = `获取验证码`
			iscountDown = false
			return
		}
		span.innerHTML = `${countDown}s后重新获取`
	}, 1000)
})

/* // 封装手机号函数
function phone(){
    const reg=/^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    const flag=reg.test(input1.value)
    const error=document.querySelector('.error')
    error.style.display = flag ? 'none' : 'block'
    return flag
}

// 封装获取验证码函数
function code(){
      const reg=/^\d{6}$/
      const flag=reg.test(input.value)
      // console.log(flag)
      const error1=input2.nextElementSibling
      error1.style.display = flag ? 'none' : 'block'
      return flag
}
 */
